'use strict';

module.exports = async function factory (app, conf) {
   try {
      if (conf.redis) {
         // https://github.com/luin/ioredis
         const redis = require("ioredis")

         app.fs = app.fs || {}
         const client = new redis(conf.redis)

         client.on("error", function (err) {
            app.fs.logger.error('[FS-REDIS]', `redis connect error ${conf.redis}`);
            app.fs.logger.error(err)
         });

         client.on('connect', function () {
            app.fs.logger.info(`redis connect success ${conf.redis}`);
         })

         // 自定义方法
         client.hdelall = async (key) => {
            const obj = await client.hgetall(key);
            await client.hdel(key, Object.keys(obj))
         }
         app.fs.redis = client
      }
   } catch (error) {
      console.error(error)
      process.exit(-1)
   }

}
